package com.chenl.provider.util.ScanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

@Component
public class InterFaceScanService {
    @Autowired
    InterfaceScanner interfaceScanner;

    public Set<Class> scanInterFaceByAnno(String packageName,Class annoClass){
        Set<Class> classes=new HashSet<>();
        interfaceScanner.addIncludeFilter(new AnnotationTypeFilter(annoClass));
        Set<BeanDefinition> beanDefinitions = interfaceScanner.findCandidateComponents(packageName);
        for (BeanDefinition beanDefinition : beanDefinitions) {
            try {
                classes.add(Class.forName(beanDefinition.getBeanClassName()));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        return classes;
    }
}
